package com.example.demo.api;

import com.example.demo.entity.domain.Teacher;
import com.example.demo.entity.enums.CodeEnum;
import com.example.demo.service.TeacherService;
import com.example.demo.util.RUtil;
import com.example.demo.vo.QueryObj;
import com.example.demo.vo.R;
import com.example.exceptions.RException;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;




@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {

    @Resource
    private TeacherService teacherService;


    /**
     * 查询所有教师
     * @return
     */
    @GetMapping("/list")
    public List<Teacher> getAllTeacher(){
        List<Teacher> teacherList = this.teacherService.findAllTeacher();
        return teacherList;
    }

    /**
     * 根据教师姓名查询
     * @param teacherName
     * @return
     */
    @RequestMapping("/search")
    public List<Teacher> search(String teacherName){
        List<Teacher> teacherList = this.teacherService.findByNameLike("%"+teacherName+"%");
        return teacherList;
    }

    /**
     * 保存学生信息
     * @param teacher
     */
    @PostMapping("/saveInfo")
    public R saveInfo(Teacher teacher){
         this.teacherService.saveInfo(teacher);
         return RUtil.success();
    }

    /**
     * 删除信息
     * @param teacher
     */
    @RequestMapping("/delData")
    public void delData(Teacher teacher){
        this.teacherService.deleteTeacher(teacher);
    }

    /**
     * 登录验证
     * @param sn
     * @param password
     * @return
     */
    @GetMapping("/validate")
    public R<Teacher> validate(String sn,String password){
        Teacher teacher = this.teacherService.validateNameAndPassword(sn, password);
        return RUtil.success(teacher);
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/getByPage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> query) {

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Integer pageIndex = 0;
        Integer pageSize = 10;

        if (query == null) {
            // 名字为空就直接调用分页
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Teacher> teachers = teacherService.getAll(pageable);
            return RUtil.success(teachers.getContent(),teachers.getTotalElements());
        } else{
            if (query.getData() instanceof Teacher){
                Pageable pageable = PageRequest.of(query.getPage()-1,query.getLimit(),sort);
                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();
                Example<Teacher> example=Example.of(query.getData(),matcher);
                Page<Teacher> teacherPage = teacherService.getAll(example, pageable);
                return RUtil.success(teacherPage.getContent(),teacherPage.getTotalElements());
            }else {
                throw new RException(CodeEnum.PARAMETER_ERROR);
            }
        }
    }

    /**
     * 批量删除信息
     * @param ids
     * @return
     */
    @PostMapping("/delDataBatch")
    public R delDtaListBatch(String ids){
        this.teacherService.delDtaListBatch(ids);
        return RUtil.success();
    }


}
